package com.lingz.manager.modules.scheduled;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lingz.manager.modules.wuxue.mapper.HuangshiDataMapper;
import com.lingz.manager.modules.wuxue.mapper.YikemanDataMapper;
import com.lingz.manager.modules.wuxue.entity.HuangshiData;
import com.lingz.manager.modules.wuxue.entity.YikemanData;
import com.lingz.manager.modules.wuxue.utils.DeviceDataUtil;
import com.lingz.manager.modules.wuxue.utils.SendRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


@Component
@Slf4j
public class Task {

    @Value("${shujujiekou.yikeman}")
    String yikemanUrl;
    @Autowired
    HuangshiDataMapper dataInfoMapper;

    @Autowired
    YikemanDataMapper yikemanDataMapper;

    @Scheduled(cron = "0 0 0/2 * * ?")
    public void huangshiDataAdd() {
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取明天的日期
        LocalDate tomorrow = today.plusDays(1);
        // 创建日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = today.format(formatter);
        String formattedDate2 = tomorrow.format(formatter);
        String huangshiUrl = "https://www.cebaowang.com:8089/external/data/upload?deviceId=YBQ0000145&token=5d95fba9a30e4bb9a6b888d847772200" +
                "&startDate=" +
                formattedDate +
                "&endDate=" +
                formattedDate2;
        SendRequest request = new SendRequest();
        String jsonResponse = request.sendGet(huangshiUrl);

        JSONObject responseJson = JSONObject.parseObject(jsonResponse);
        if (responseJson != null && responseJson.containsKey("data")){
            JSONArray data = responseJson.getJSONArray("data");
            if(data!= null && !data.isEmpty()){
                JSONObject jsonObject = data.getJSONObject(0);
                try {
                    HuangshiData huangshiData = DeviceDataUtil.parseJsonToHSWeatherData(jsonObject);
                    log.info("准备插入一条黄氏设备记录");
                    dataInfoMapper.insert(huangshiData);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void yikemanInsectDataAdd() {
        SendRequest request = new SendRequest();
        String jsonResponse = request.sendGet(yikemanUrl);
        JSONObject responseJson = JSONObject.parseObject(jsonResponse);
        if (responseJson != null && responseJson.containsKey("resultData")){
            JSONArray data = responseJson.getJSONArray("resultData");
            if(data!= null && !data.isEmpty()){
                JSONObject jsonObject = data.getJSONObject(data.size() - 1);
                try {
                    YikemanData yikemanData = DeviceDataUtil.parseJsonToYKMInsectData(jsonObject);
                    log.info("准备插入一条依科曼设备❤诱数据记录");
                    yikemanDataMapper.insert(yikemanData);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
